from math import ceil

import numpy as np
from openpyxl import Workbook

from osc02.static.constants import Constants


class RandomUtil:
    # n_sf: number of standard floor
    def __init__(self, seed=5, n_sf=Constants.N_ACTIVITY):
        self.seed = seed
        self.n_sf = n_sf
        self.sf_erection_durations = None

        self.component_delivery_delays = None

    def get_component_delivery_delays(self):
        if self.component_delivery_delays is None:
            self.generate_component_delivery_delays()
        return self.component_delivery_delays

    def generate_crane_failure(self):
        np.random.seed(self.seed)
        self.component_delivery_delays = \
            np.random.normal(Constants.COMPONENT_DELIVERY_DELAY_LOC,
                             Constants.COMPONENT_DELIVERY_DELAY_SCALE,
                             self.n_sf)

    def generate_component_delivery_delays(self):
        np.random.seed(self.seed)
        self.component_delivery_delays = \
            np.random.normal(Constants.COMPONENT_DELIVERY_DELAY_LOC,
                             Constants.COMPONENT_DELIVERY_DELAY_SCALE,
                             self.n_sf)

    # return durations of all floors
    def get_sf_erection_durations(self):
        if self.sf_erection_durations is None:
            self.generate_sf_erection_durations()
        return self.sf_erection_durations

    def generate_sf_erection_durations(self):
        np.random.seed(self.seed)
        self.sf_erection_durations = []
        # floor 1 and 2
        self.sf_erection_durations.extend(
            np.random.triangular(Constants.STANDARD_FLOOR_ERECTION_LEFT + 1,
                                 Constants.STANDARD_FLOOR_ERECTION_MODE + 1,
                                 Constants.STANDARD_FLOOR_ERECTION_RIGHT + 1,
                                 2))
        # print(self.sf_erection_durations)
        # floor 3 to n-3
        self.sf_erection_durations.extend(
            np.random.triangular(Constants.STANDARD_FLOOR_ERECTION_LEFT,
                                 Constants.STANDARD_FLOOR_ERECTION_MODE,
                                 Constants.STANDARD_FLOOR_ERECTION_RIGHT,
                                 self.n_sf - 3))
        # print(self.sf_erection_durations)
        # top floor
        self.sf_erection_durations.extend(
            np.random.triangular(Constants.STANDARD_FLOOR_ERECTION_LEFT + 1,
                                 Constants.STANDARD_FLOOR_ERECTION_MODE + 1,
                                 Constants.STANDARD_FLOOR_ERECTION_RIGHT + 1,
                                 1))
        # print(self.sf_erection_durations)

    def to_excel(self, filename, data_list):
        pass


if __name__ == "__main__":
    random_util = RandomUtil(seed=1)
    # filename = 'actual durations' + '.xlsx'
    # if os.path.exists(filename):
    #     os.remove(filename)
    #
    # wb = Workbook()
    # # ws = wb.remove_sheet
    # ws = wb.active
    # title = ['seed']
    # for i in range(1, Constants.N_ACTIVITY + 1):
    #     title.append('FLR' + str(i).zfill(2))
    # ws.append(title)
    # for seed in range(1, 31):
    #     random_util.seed = seed
    #     act_duration = [seed]
    #     # random_util.generate_sf_erection_durations()
    #     random_util.generate_sf_erection_durations()
    #     durations = random_util.get_sf_erection_durations()
    #     act_duration.extend(durations)
    #     ws.append([ceil(x) for x in act_duration])
    #
    # wb.save(filename)
    # wb.close()
    # print(act_duration)
    filename = 'act_delivery_delay' + '.xlsx'
    wb = Workbook()
    # ws = wb.remove_sheet
    ws = wb.active
    title = ['seed']
    for i in range(1, Constants.N_ACTIVITY + 1):
        title.append('FLR' + str(i).zfill(2))
    ws.append(title)
    for seed in range(1, 21):
        random_util.seed = seed
        random_util.generate_component_delivery_delays()
        act_delivery_delay = [seed]
        act_delivery_delay.extend(random_util.get_component_delivery_delays())
        print([' ' if x < 0 else ceil(x) for x in act_delivery_delay])
        ws.append([' ' if x < 0 else ceil(x) for x in act_delivery_delay])
    wb.save(filename)
    wb.close()
